﻿/*
   Copyright 2012 University of Southampton

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;

namespace uk.ac.soton.ses.ExperimentActionsWebPart
{
    [ToolboxItemAttribute(false)]
    public class ExperimentActionsWebPart : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/uk.ac.soton.ses/HDCUserControls/ExperimentActionsWebPartUserControl.ascx";

        private ExperimentActionsWebPartUserControl _experimentActionsControl;
        //IWebPartRow _provider;
        //private DataRowView _dataRowView;

        private IHDCWebPartSelectedExperimentID _experimentProvider = null;

        //Always load the control in OnInit() so we can process postbacks but set it as invisible in case the provider tells us we have no experiment to bind to.
        //We'll make it visible again in OnPreRender as that is the only stage we'll definitely have a provider.
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this._experimentActionsControl = (ExperimentActionsWebPartUserControl)Page.LoadControl(_ascxPath);
            this._experimentActionsControl.Visible = false;
            this.Controls.Add(_experimentActionsControl);
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (this._experimentProvider == null)
            {
                this.DisplayMessage("No connection. Please connect me to an Experiment list.");
            }
            else
            {
                int experimentID = this._experimentProvider.GetSelectedExperimentID();
                if (experimentID > 0)
                {
                    this._experimentActionsControl.Visible = true;
                    this._experimentActionsControl.ExperimentID = experimentID;
                }
                else
                {
                    this.DisplayMessage("Please select an experiment.");
                }
                    
            }
            //if (_provider == null)
            //{
            //    DisplayMessage("No connection. Please connect me to an Experiment list.");
            //}
            //else
            //{
            //    if (_dataRowView == null)
            //    {
            //        DisplayMessage("No data.");
            //    }
            //    else
            //    {
            //        DataRow datarow = _dataRowView.Row;
            //        int id = int.Parse(datarow["ID"].ToString());
            //        _experimentActionsControl.BindToExperiment(int.Parse(datarow["ID"].ToString()));
            //    }
            //}
        }

        [ConnectionConsumer("Selected experiment", "SelectedExperiment")]
        public void SetExperimentConnectionInterface(IHDCWebPartSelectedExperimentID experimentProvider)
        {
            this._experimentProvider = experimentProvider;
        }

/*
        [ConnectionConsumer("Row")]
        public void SetConnectionInterface(IWebPartRow provider)
        {
            this._provider = provider;
            if (_provider == null)
            {
                DisplayMessage("No connection. Please connect me to an Experiment Data Files list.");
            }
            else
            {
                _provider.GetRowData(new RowCallback(GetRowDataCallback));
            }
        }
        public void GetRowDataCallback(object rowData)
        {
            try
            {
                _dataRowView = (DataRowView)rowData;
            }
            catch (Exception e)
            {
                _dataRowView = null;
                DisplayMessage(String.Format("Exception {0}", e.Message));
            }
        }
 */
        private void DisplayMessage(string error)
        {
            Label messageLabel = new Label();
            Controls.Add(messageLabel);
            messageLabel.Text = error;
        }
    }
}
